package com.yatb.trac.controller.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.yatb.trac.pojo.wrappers.UtentiView;

public class ChangePasswordValidator implements Validator {

	public boolean supports(Class<?> clazz) {
		return clazz.isAssignableFrom(UtentiView.class);
	}

	public void validate(Object target, Errors errors) {
		UtentiView user = (UtentiView) target;
		ValidationUtils.rejectIfEmpty(errors, "newPassword", "", "Campo obbligatorio");
		ValidationUtils.rejectIfEmpty(errors, "repeatPassword", "", "Campo obbligatorio");
		if(!errors.hasErrors()){
			if(!user.getNewPassword().equals(user.getRepeatPassword())){
				errors.rejectValue("repeatPassword", "","La password non corrisponde");
			}
		}
	}

}
